/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.relate.relateop;

import java.util.List;

import chunmap.model.coord.Position;
import chunmap.model.elem.PointLineBag;
import chunmap.model.geom.GeoPoint;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.LineString;
import chunmap.model.relate.ComputeIm;
import static chunmap.model.relate.IntersectionMatrix.*;

/**
 * disjion,over,touch,with,contain,equals
 * 
 * @author chunquedong
 * 
 */
public class LineString_LineString extends ComputeIm {

	public LineString_LineString(LineString l1, LineString l2) {
		g1 = l1;
		g2 = l2;
	}

	@Override
	protected int inner2innerDim(Geometry g1, Geometry g2) {
		LineString l1 = (LineString) g1;
		LineString l2 = (LineString) g2;

		PointLineBag bag = l1.intersection(l2);

		if (bag.getLineStringSize() > 0)
			return LineDim;
		else if (bag.getPointSize() > 0) {
			if (isInner(l1, l2, bag.getPointList())) {
				return PointDim;
			}
		}
		return EmptyDim;
	}

	private boolean isInner(LineString l1, LineString l2, List<GeoPoint> points) {
		for (GeoPoint p : points) {
			if (!isBorder(l1, l2, p.getCoordinate())) {
				return true;
			}
		}
		return false;
	}

	// 是边界点
	private boolean isBorder(LineString l1, LineString l2, Position p) {
		return p.equals(l1.firstPoint()) || p.equals(l1.lastPoint())
				|| p.equals(l2.firstPoint()) || p.equals(l2.lastPoint());
	}

	// ------------------------------------------------------------

	@Override
	protected boolean within(Geometry g1, Geometry g2) {
		if (im.get(Inner, Inner) != LineDim)
			return false;

		LineString l1 = (LineString) g1;
		LineString l2 = (LineString) g2;

		if (l2.containLineString(l1)) {
			return true;
		}

		return false;
	}

}
